<template>
  <el-cascader
    :options="dataTree"
    :props="props"
    size="mini"
    :value="value"
    separator="-"
    @change="changeValue"
  />
</template>
<script>
import { getDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils'
export default {
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      dataTree: [],
      props: {
        label: 'name',
        // children: 'children',
        value: 'id'
      }
    }
  },
  created() {
    this.departmentTree()
  },
  methods: {
    // 获取部门树形数据
    async departmentTree() {
      this.dataTree = transListToTreeData(await getDepartment(), 0)
    },
    changeValue(list) {
      if (list.length > 0) {
        this.$emit('input', list[list.length - 1])
      } else {
        this.$emit('input', null)
      }
    }
  }
}
</script>
